﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM_SharePoint_Transports
{
    /// <summary>
    /// SharePoint file send transport
    /// </summary>
    public class SharePointFileSendTransport : BlueIntegrator.Extensibility.SendTransportBase
    {
        public override void Initialise(string portId, string transportName, System.Collections.IDictionary transportParameters)
        {
            m_portId = portId;
            m_dal = transportParameters["__DAL"] as BlueIntegrator.DAL.DALBase;
            m_spcf = transportParameters["__RPCF"] as BlueIntegrator.Messaging.SendPortConfigurationForm;
            m_properties = new System.Collections.Hashtable();
            m_url = BlueIntegrator.Messaging.SettingTokenHelper.ResolveTokens(m_dal, transportParameters["SPURL"]);
            m_destURL = BlueIntegrator.Messaging.SettingTokenHelper.ResolveTokens(m_dal, transportParameters["DestinationURL"]);
            m_user = BlueIntegrator.Messaging.SettingTokenHelper.ResolveTokens(m_dal, transportParameters["SPUser"]);
            m_password = BlueIntegrator.Messaging.SettingTokenHelper.ResolveTokens(m_dal, BlueIntegrator.Helpers.Encryption.DecryptString(transportParameters["SPPwd"] as string, "SSIS"));
            m_timeout = BlueIntegrator.Helpers.Conversion.SafeGetInt(transportParameters["SPTimeOut"], 60);

            m_api = new SSISLib.SharePointListAPI();
        }

        string m_url, m_destURL, m_user, m_password;
        int m_timeout;

        public override System.IO.Stream SendMessage(System.IO.Stream messageContents, System.Collections.IDictionary contextParameters)
        {
            // form properties
            List<SPCopy.FieldInformation> properties = new List<SPCopy.FieldInformation>();

            for (int i = 0; i < BlueIntegrator.Helpers.Conversion.SafeGetInt(m_properties["NumSPPropertyBindings"]); i++)
            {
                string binding = m_properties["SPPropertyBinding:" + i + ":Binding"] as string ?? "?";

                if (binding.Contains("%") && contextParameters != null)
                {
                    foreach (System.Collections.DictionaryEntry dent in contextParameters)
                    {
                        if (binding.Contains(dent.Key.ToString()))
                        {
                            binding= binding.Replace("%" + dent.Key + "%", BlueIntegrator.Helpers.Conversion.SafeGetString(dent.Value));
                        }
                    }
                }

                SPProperty prop = new SPProperty(m_properties["SPPropertyBinding:" + i + ":DisplayName"] as string ?? "?", m_properties["SPPropertyBinding:" + i + ":InternalName"] as string ?? "?", m_properties["SPPropertyBinding:" + i + ":FieldType"] as string ?? "?", binding);
                properties.Add(new SPCopy.FieldInformation() { DisplayName = prop.DisplayName, InternalName = prop.InternalName, Type = prop.Type, Value = prop.Binding });
            }

            SharePointFileAPI.UploadFile(m_destURL, m_url, m_user, m_password, m_timeout, m_dal, properties.ToArray(), BlueIntegrator.DAL.DALBase.ConvertToMemoryStream(messageContents).ToArray());
            return null;
        }

        public override System.Windows.Forms.Control[] GetTabPages()
        {
            return new System.Windows.Forms.Control[] {new SharePointFileTransportConfigControl(m_spcf)};
        }

        System.Collections.Hashtable m_properties = new System.Collections.Hashtable();
        string m_portId;
        BlueIntegrator.DAL.DALBase m_dal;
        SSISLib.SharePointListAPI m_api;
        BlueIntegrator.Messaging.SendPortConfigurationForm m_spcf;
    }
}
